// Triangle.cpp: implementation of the CTriangle class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "DrawGraph.h"
#include "Triangle.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CTriangle::CTriangle()
{
	
}
CTriangle::CTriangle(CPoint StartPoint,CPoint EndPoint)
{
	m_pt[0] = StartPoint;
	int len = (int)sqrt( (StartPoint.x-EndPoint.x)*(StartPoint.x-EndPoint.x) + 
		(StartPoint.y-EndPoint.y)*(StartPoint.y-EndPoint.y) )/2;
	m_pt[1] = EndPoint; m_pt[1].x -= len;
	m_pt[2] = EndPoint; m_pt[2].x += len;
	if( m_rgn.m_hObject )
		m_rgn.DeleteObject();
	m_rgn.CreatePolygonRgn( m_pt, 3, ALTERNATE );

	SetTheShapeGravity( );
	ReSetArea();
	ReSetCircum();
	m_SumCircum +=this->circum;
	m_SumArea += this->area;

	
}
CTriangle::~CTriangle()
{
	m_SumCircum -=this->circum;
	m_SumArea -= this->area;

}
void CTriangle::ReSetCircum( )
{
	double c;
	c = sqrt( (m_pt[0].x-m_pt[1].x)*(m_pt[0].x-m_pt[1].x) + 
		(m_pt[0].y-m_pt[1].y)*(m_pt[0].y-m_pt[1].y) );
	c += sqrt( (m_pt[0].x-m_pt[2].x)*(m_pt[0].x-m_pt[2].x) + 
		(m_pt[0].y-m_pt[2].y)*(m_pt[0].y-m_pt[2].y) );
	c += sqrt( (m_pt[2].x-m_pt[1].x)*(m_pt[2].x-m_pt[1].x) + 
		(m_pt[2].y-m_pt[1].y)*(m_pt[2].y-m_pt[1].y) );
	this->circum = (int)c;
}
void CTriangle::ReSetArea( )
{
	double a;
	a = fabs(fabs(m_pt[0].y-m_pt[2].y) * fabs(m_pt[1].x-m_pt[0].x)
		+ fabs(m_pt[2].x-m_pt[0].x) * fabs(m_pt[1].y-m_pt[2].y)
		- fabs(m_pt[0].x-m_pt[2].x) * fabs(m_pt[0].y-m_pt[2].y)) / 2;
	this->area = a;
}
void CTriangle::SetTheShapeGravity( )
{
	int x,y;
	x= (m_pt[0].x + m_pt[1].x + m_pt[2].x)/3;
	y = (m_pt[0].y + m_pt[1].y + m_pt[2].y)/3;
	this->m_CenterOfGravity = CPoint(x,y);
}

void CTriangle::Zoom(BOOL zoom,UINT x)
{
	if(zoom)
	{
		m_pt[0].x -=5*x;
		m_pt[0].y -=5*x;
		m_pt[1].x -=5*x;
		m_pt[1].y +=5*x;
		m_pt[2].x +=5*x;
		m_pt[2].y +=5*x;
	}
	else
	{
		m_pt[0].x +=5*x;
		m_pt[0].y +=5*x;
		m_pt[1].x +=5*x;
		m_pt[1].y -=5*x;
		m_pt[2].x -=5*x;
		m_pt[2].y -=5*x;
	}
	if( m_rgn.m_hObject )
		m_rgn.DeleteObject();
	m_rgn.CreatePolygonRgn(m_pt, 3, ALTERNATE );
	SetTheShapeGravity();
}
void CTriangle::SetTheShapeInfor(COLORREF clr ,
					  UINT width,BOOL IsSolid )
{
	this->m_clr = clr;
	this->m_nLineWidth = width;
	this->m_BrushIsSolid = IsSolid;
}
void CTriangle::MoveReset(const CPoint &p)
{
	for ( int i=0; i<3; i++ )
		m_pt[i] -= p;
	if ( m_rgn.m_hObject )
		m_rgn.DeleteObject();
	m_rgn.CreatePolygonRgn( m_pt, 3, ALTERNATE );
	SetTheShapeGravity();
}
void CTriangle::ResetTheShape(const CPoint &p1,const CPoint &p2)
{
	m_pt[0] = p1;
	int len = (int)sqrt( (p1.x-p2.x)*(p1.x-p2.x) + (p1.y-p2.y)*(p1.y-p2.y) )/2;
	m_pt[1] = p2; m_pt[1].x -= len;
	m_pt[2] = p2; m_pt[2].x += len;
	if( m_rgn.m_hObject )
		m_rgn.DeleteObject();
	m_rgn.CreatePolygonRgn( m_pt, 3, ALTERNATE );
	SetTheShapeGravity();
}